package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.StarOverviewActivity;
import flc.ast.bean.StarsBean;
import flc.ast.databinding.FragmentHomeBinding;
import flc.ast.dialog.FortuneDialog;
import stark.common.apis.ApiManager;
import stark.common.apis.base.ConstInfoBean;
import stark.common.apis.base.ConstMonthBean;
import stark.common.apis.base.ConstTodayBean;
import stark.common.apis.base.ConstWeekBean;
import stark.common.apis.base.ConstYearBean;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    public static String starsName;//星座名称
    private List<StarsBean> listStar = new ArrayList<>();
    private int type = 0;
    private String[] fortunes;
    private String[] tips;
    private int selPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivStarSignImg.setOnClickListener(this);
        mDataBinding.tvFortuneSel.setOnClickListener(this);
        mDataBinding.ivStarSignToggle.setOnClickListener(this);

        //设置选择的星座名称 第一次进入时默认为白羊座
        starsName = SPUtil.getString(mContext, AppConfig.Name, getString(R.string.star_sign1));

        fortunes = getContext().getResources().getStringArray(R.array.fortune);
        tips = getContext().getResources().getStringArray(R.array.tip);
        mDataBinding.tvStarSignTip.setText(tips[selPosition]);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivStarSignImg://切换星座
                startActivity(StarOverviewActivity.class);
                break;
            case R.id.tvFortuneSel://切换运势类型
                showFortuneDialog();
                break;
            case R.id.ivStarSignToggle://切换提示
                if (selPosition == tips.length - 1) {
                    selPosition = 0;
                } else {
                    selPosition++;
                }
                mDataBinding.tvStarSignTip.setText(tips[selPosition]);
                break;
        }
    }

    @Override
    protected void initData() {
        listStar.add(new StarsBean(getString(R.string.star_sign11), R.drawable.spz, getString(R.string.birth_date11)));
        listStar.add(new StarsBean(getString(R.string.star_sign12), R.drawable.syz, getString(R.string.birth_date12)));
        listStar.add(new StarsBean(getString(R.string.star_sign1), R.drawable.byz, getString(R.string.birth_date1)));
        listStar.add(new StarsBean(getString(R.string.star_sign2), R.drawable.jnz, getString(R.string.birth_date2)));
        listStar.add(new StarsBean(getString(R.string.star_sign3), R.drawable.shuangzz, getString(R.string.birth_date3)));
        listStar.add(new StarsBean(getString(R.string.star_sign4), R.drawable.jxz, getString(R.string.birth_date4)));
        listStar.add(new StarsBean(getString(R.string.star_sign5), R.drawable.szz, getString(R.string.birth_date5)));
        listStar.add(new StarsBean(getString(R.string.star_sign6), R.drawable.cnz, getString(R.string.birth_date6)));
        listStar.add(new StarsBean(getString(R.string.star_sign7), R.drawable.tcz, getString(R.string.birth_date7)));
        listStar.add(new StarsBean(getString(R.string.star_sign8), R.drawable.txz, getString(R.string.birth_date8)));
        listStar.add(new StarsBean(getString(R.string.star_sign9), R.drawable.ssz, getString(R.string.birth_date9)));
        listStar.add(new StarsBean(getString(R.string.star_sign10), R.drawable.mxz, getString(R.string.birth_date10)));
    }

    @Override
    public void onResume() {
        super.onResume();
        setData();
    }

    private void setData() {
        if (type == 0) {
            getConstToday();
            getStarsAnalysis();
            setTodayDate();
            mDataBinding.llDayData.setVisibility(View.VISIBLE);
            mDataBinding.llFortune.setVisibility(View.VISIBLE);
        } else if (type == 1) {
            getConstTomorrow();
            getStarsAnalysis();
            setTomorrowDate();
            mDataBinding.llDayData.setVisibility(View.VISIBLE);
            mDataBinding.llFortune.setVisibility(View.VISIBLE);
        } else if (type == 2) {
            getConstWeek();
            getStarsAnalysis();
            setTodayDate();
            mDataBinding.llDayData.setVisibility(View.GONE);
            mDataBinding.llFortune.setVisibility(View.GONE);
        } else if (type == 3) {
            getConstMonth();
            getStarsAnalysis();
            setTodayDate();
            mDataBinding.llDayData.setVisibility(View.GONE);
            mDataBinding.llFortune.setVisibility(View.GONE);
        } else if (type == 4) {
            getConstYear();
            getStarsAnalysis();
            setTodayDate();
            mDataBinding.llDayData.setVisibility(View.GONE);
            mDataBinding.llFortune.setVisibility(View.GONE);
        }
        mDataBinding.tvFortuneSel.setText(fortunes[type]);
        setTextAndImg();
    }

    //获取今日势运
    private void getConstToday() {
        ApiManager.constApi().getConstToday(this, starsName, new IReqRetCallback<ConstTodayBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstTodayBean result) {
                if (success) {
                    setTodayData(result);
                    setPairStar(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
    }

    //设置今日势运
    private void setTodayData(ConstTodayBean result) {
        mDataBinding.rbLove.setRating(Integer.parseInt(result.getLove()) / 20);
        mDataBinding.rbWealth.setRating(Integer.parseInt(result.getMoney()) / 20);
        mDataBinding.rbWork.setRating(Integer.parseInt(result.getWork()) / 20);
        mDataBinding.rbHealth.setRating(Integer.parseInt(result.getHealth()) / 20);
        mDataBinding.tvSummarizeSel.setText(getString(R.string.jrgs));
        mDataBinding.tvSummarize.setText(result.getSummary());
    }

    //获取星座解析
    private void getStarsAnalysis() {
        if (!TextUtils.isEmpty(starsName)) {
            ApiManager.constApi().getConstInfo(this, starsName, new IReqRetCallback<ConstInfoBean>() {
                @Override
                public void onResult(boolean success, String msg, @Nullable ConstInfoBean result) {
                    if (success) {
                        setAnalysisData(result);
                    } else {
                        ToastUtils.showShort(getString(R.string.is_limit));
                    }
                }
            });
        }
    }

    //设置星座解析
    private void setAnalysisData(ConstInfoBean result) {
        mDataBinding.tvNum.setText(result.getXyhm());
        mDataBinding.tvColor.setText(result.getXyys().substring(0, 1));
        mDataBinding.tvGood.setText(result.getYd());
        mDataBinding.tvBad.setText(result.getQd());
        mDataBinding.tvJbbx.setText(result.getBx());
        mDataBinding.tvJbtz.setText(result.getJbtz());
        mDataBinding.tvJttz.setText(result.getJttz());
        mDataBinding.tvXsfg.setText(result.getXsfg());
        mDataBinding.tvZtpj.setText(result.getZj());
    }

    private void setTextAndImg() {
        for (StarsBean starsBean : listStar) {
            if (starsBean.getName().equals(starsName)) {
                mDataBinding.ivStarSignImg.setImageResource(starsBean.getImg());
                mDataBinding.tvStarSignName.setText(starsBean.getName());
                mDataBinding.tvStarSignTime.setText(starsBean.getDate());
            }
        }
    }

    //设置今日日期
    private void setTodayDate() {
        String year = TimeUtils.getNowString(new SimpleDateFormat("yyyy EEE"));
        mDataBinding.tvYear.setText(year);
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        mDataBinding.tvMonth.setText((month + 1) + "");
        mDataBinding.tvDay.setText(day + "");
    }

    //速配星座
    private void setPairStar(ConstTodayBean result) {
        for (StarsBean starsBean : listStar) {
            if (starsBean.equals(result.getQFriend())) {
                mDataBinding.ivPairStar.setImageResource(starsBean.getImg());
            }
        }
    }

    //获取明日势运
    private void getConstTomorrow() {
        ApiManager.constApi().getConstTomorrow(this, starsName, new IReqRetCallback<ConstTodayBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstTodayBean result) {
                if (success) {
                    setTomorrowData(result);
                    setPairStar(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
    }

    //设置明日势运
    private void setTomorrowData(ConstTodayBean result) {
        mDataBinding.rbLove.setRating(Integer.parseInt(result.getLove()) / 20);
        mDataBinding.rbWealth.setRating(Integer.parseInt(result.getMoney()) / 20);
        mDataBinding.rbWork.setRating(Integer.parseInt(result.getWork()) / 20);
        mDataBinding.rbHealth.setRating(Integer.parseInt(result.getHealth()) / 20);
        mDataBinding.tvSummarizeSel.setText(getString(R.string.jrgs));
        mDataBinding.tvSummarize.setText(result.getSummary());
    }

    //设置明日日期
    private void setTomorrowDate() {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(year, month, day + 1);
        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DAY_OF_MONTH);
        mDataBinding.tvYear.setText(year + "");
        mDataBinding.tvMonth.setText((month + 1) + "");
        mDataBinding.tvDay.setText(day + "");
    }

    //获取本周势运
    private void getConstWeek() {
        ApiManager.constApi().getConstWeek(this, starsName, new IReqRetCallback<ConstWeekBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstWeekBean result) {
                if (success) {
                    setWeekData(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
    }

    //设置本周势运
    private void setWeekData(ConstWeekBean result) {
        mDataBinding.tvSummarizeSel.setText(getString(R.string.bzgs));
        mDataBinding.tvSummarize.setText(result.getHealth() + result.getLove() + result.getMoney() + result.getWork());
    }

    //获取本月势运
    private void getConstMonth() {
        ApiManager.constApi().getConstMonth(this, starsName, new IReqRetCallback<ConstMonthBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstMonthBean result) {
                if (success) {
                    seMonthData(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
    }

    //设置本月势运
    private void seMonthData(ConstMonthBean result) {
        mDataBinding.tvSummarizeSel.setText(getString(R.string.bygs));
        mDataBinding.tvSummarize.setText(result.getAll());
    }

    //获取本月势运
    private void getConstYear() {
        ApiManager.constApi().getConstYear(this, starsName, new IReqRetCallback<ConstYearBean>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable ConstYearBean result) {
                if (success) {
                    setYearData(result);
                } else {
                    ToastUtils.showShort(getString(R.string.is_limit));
                }
            }
        });
    }

    //设置全年势运
    private void setYearData(ConstYearBean result) {
        mDataBinding.tvSummarizeSel.setText(getString(R.string.qngs));
        mDataBinding.tvSummarize.setText(result.getCareer().toString() + result.getLove().toString() + result.getHealth().toString() + result.getFinance().toString());
    }

    private void showFortuneDialog() {
        FortuneDialog fortuneDialog = new FortuneDialog(mContext, type);
        fortuneDialog.setListener(new FortuneDialog.Listener() {
            @Override
            public void click(int position) {
                type = position;
                setData();
            }
        });
        fortuneDialog.show();
    }

}